package com.usian.controller;

import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@RestController
public class ArticleController {

    @Qualifier("client")
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @RequestMapping("insert")
    public String insert(){

        try {
            HashMap<String,Object> map =new HashMap<>();
            map.put("id", "66");
            map.put("publishTime", new Date());
            map.put("layout", 66);
            map.put("images", "66");
            map.put("authorId", 66);
            map.put("title", "66");
            map.put("content","66");
            //1:索引库名字
            IndexRequest indexRequest =new IndexRequest("app_info_article").source(map);
            restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "SUCC";
    }

    @RequestMapping("batchInsert")
    public String batchInsert(){

        try {

            //1:索引库名字

            BulkRequest bulkRequest =new BulkRequest();
            for(int i=0;i<=1;i++){
                HashMap<String,Object> map =new HashMap<>();
                map.put("id", "66"+i);
                map.put("publishTime", new Date());
                map.put("layout", 66+i);
                map.put("images", "66"+i);
                map.put("authorId", 66+i);
                map.put("title", "66"+i);
                map.put("content","66"+i);
                IndexRequest indexRequest =new IndexRequest("app_info_article").source(map);
                bulkRequest.add(indexRequest);
            }

            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "SUCC";
    }


    @RequestMapping("update")
    public String update(){

        try {
            HashMap<String,Object> map =new HashMap<>();
            map.put("id", "667");
            map.put("publishTime", new Date());
            map.put("layout", 667);
            map.put("images", "667");
            map.put("authorId", 667);
            map.put("title", "667");
            map.put("content","667");
            //1:索引库名字
            IndexRequest indexRequest =new IndexRequest("app_info_article").id("66").source(map);
            restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "SUCC";
    }


    @RequestMapping("search")
    public String search(){
        List<String> resultMap =new ArrayList<>();
        try {
            //根据title 和content 关系 or  and  publishDate<? 查询



            //1:SearchRequest 构建第一层查询条件
            SearchRequest searchRequest =new SearchRequest("app_info_article");

            //2：SearchSourceBuilder 构建第二层对象
            SearchSourceBuilder searchSourceBuilder =new SearchSourceBuilder();

            BoolQueryBuilder boolQueryBuilder =new BoolQueryBuilder();


            //根据title 和content
            QueryStringQueryBuilder stringQueryBuilder = QueryBuilders.queryStringQuery("660").field("title").field("content").defaultOperator(Operator.OR);

            boolQueryBuilder.must(stringQueryBuilder);

            //publishDate<?
            RangeQueryBuilder rangeQueryBuilder =QueryBuilders.rangeQuery("publishTime").lt(new Date());

            boolQueryBuilder.filter(rangeQueryBuilder);

            searchSourceBuilder.query(boolQueryBuilder);

            //排序
            searchSourceBuilder.sort("publishTime", SortOrder.DESC);
            //分页
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(2);

            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);


            //解析结果
            SearchHit[] hits = searchResponse.getHits().getHits();

            for(SearchHit hit :hits){
                String asString  = hit.getSourceAsString();
                resultMap.add(asString);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return resultMap.toString();

    }
}
